/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DeliciousCrawler.gui;

import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import model.dao.AuthorDAO;
import model.dao.FollowingDAO;
import model.dao.LinkDAO;
import model.dao.SaveLinkDAO;
import model.dao.TagCollectDAO;
import model.dao.TagDAO;
import model.dao.TagLinkDAO;
import model.pojo.*;

/**
 *
 * @author THANHTUNG
 */
public class ShowData extends javax.swing.JDialog {

    /**
     * Creates new form ShowData
     */
    public ShowData(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    final int LINKS = 0,
            AUTHORS = 1,
            TAGS = 2,
            SAVE_LINKS = 3,
            TAG_LINKS = 4,
            FOLLOWERS = 5;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jcbTable = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbShowData = new javax.swing.JTable();
        btnRun = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jcbTable.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Links", "Authors", "Tags", "Save Links", "Tag Links", "Followers" }));

        jtbShowData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbShowData);

        btnRun.setText("Print");
        btnRun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRunActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jcbTable, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnRun, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 618, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jcbTable)
                    .addComponent(btnRun, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(36, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private Object[][] getList(int index){
        LinkDAO link = new LinkDAO();
        TagDAO tag = new TagDAO();
        AuthorDAO au = new AuthorDAO();
        SaveLinkDAO sl = new SaveLinkDAO();
        TagLinkDAO tl = new TagLinkDAO();
        FollowingDAO fd = new FollowingDAO();
        TagCollectDAO tc = new TagCollectDAO();
        Object[][] arr=null;
        System.out.println(index);
        switch (index) {
            case TAGS:
               List<Tag> list = tag.getListOrdered();
                arr = new Object[list.size()][];
               for (int i = 0;i<list.size();i++){
                   arr[i] = new Object[getColCount(index)];
                   arr[i][0] = list.get(i).getTagId();
                   arr[i][1] = list.get(i).getTagName();
               }
                break;
            case AUTHORS:
                 List<Author> listau = au.getListOrdered();
                  arr = new Object[listau.size()][];
               for (int i = 0;i<listau.size();i++){
                   arr[i] = new Object[getColCount(index)];
                   arr[i][0] = listau.get(i).getAuthorId();
                   arr[i][1] = listau.get(i).getAuthorName();
               }
                break;
            case LINKS:
                List<Link> listli = link.getListOrdered();
                arr = new Object[listli.size()][];
                System.out.println(getColCount(index));
               for (int i = 0;i<listli.size();i++){
                   arr[i] = new Object[getColCount(index)];
                   arr[i][0] = listli.get(i).getLinkId();
                   arr[i][1] = listli.get(i).getUrl();
                   arr[i][2] = listli.get(i).getTitle();
                   arr[i][3] = listli.get(i).getTotalPosts();
               }
                break;
            case SAVE_LINKS:
                List<Object[]> listsl = sl.getListOrdered();
                arr = new Object[listsl.size()][];
                System.out.println(getColCount(index));
               for (int i = 0;i<listsl.size();i++){
                   arr[i] = new Object[getColCount(index)];
                   arr[i][0] = listsl.get(i)[0];
                   arr[i][1] = listsl.get(i)[1];
                    }
                
                break;
            case TAG_LINKS:
                List<Object[]> listtl = tl.getListOrdered();
                arr = new Object[listtl.size()][];
                System.out.println(getColCount(index));
               for (int i = 0;i<listtl.size();i++){
                   arr[i] = new Object[getColCount(index)];
                   arr[i][0] = listtl.get(i)[0];
                   arr[i][1] = listtl.get(i)[1];
                   arr[i][2] = listtl.get(i)[2];
                    }
                break;
            case FOLLOWERS:
               List<Object[]> listfd = fd.getListOrdered();
                arr = new Object[listfd.size()][];
                System.out.println(getColCount(index));
               for (int i = 0;i<listfd.size();i++){
                   arr[i] = new Object[getColCount(index)];
                   arr[i][0] = listfd.get(i)[0];
                   arr[i][1] = listfd.get(i)[1];
                  
                    }
                break;
          default:;
        }
        return arr;
    }
     private int getColCount(int index) {
              switch (index) {
            case TAGS:
               return 2;
                
            case AUTHORS:
                return 2;
                
            case LINKS:
                return 5;
            case SAVE_LINKS:
                return 2;
            case TAG_LINKS:
               return 3;
            case FOLLOWERS:
                return 2;
                
          default:return 0;
        }
            }
    private void btnRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRunActionPerformed
        
        
        TableModel table = new AbstractTableModel() {
            int index = jcbTable.getSelectedIndex();
            Object[][] arr = getList(index);
            @Override
            public int getRowCount() {
                return arr.length;
            }

            @Override
            public int getColumnCount() {
                return getColCount(index);
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return arr[rowIndex][columnIndex];
            }
        };
        jtbShowData.setModel(table);
        
    }//GEN-LAST:event_btnRunActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ShowData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ShowData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ShowData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ShowData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ShowData dialog = new ShowData(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRun;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbTable;
    private javax.swing.JTable jtbShowData;
    // End of variables declaration//GEN-END:variables
}
